package com.easychat;

import com.easychat.redis.RedisCache;
import com.easychat.websocket.netty.NettyWebSocketStarter;
import com.easychat.websocket.netty.NettyWebSocketStarter2;
import io.lettuce.core.RedisConnectionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.SQLException;

/**
 * @Author Dongdong
 * @注释  每次启动时执行 显示信息
 */
@Component("initRun")
public class InitRun implements ApplicationRunner {
    private static final Logger logger = LoggerFactory.getLogger(InitRun.class);
    @Resource
    private DataSource dataSource;
    @Resource
    private NettyWebSocketStarter  nettyWebSocketStarter;
    @Resource
    private NettyWebSocketStarter2 nettyWebSocketStarter2;
    @Override
    public void run(ApplicationArguments args) {
        try {
            dataSource.getConnection();
//            redisCache.getCacheObject("test");
            //netty 是异步启动的，如果不是异步启动，启动后，会阻塞，无法执行下面的代码
            nettyWebSocketStarter.startNetty();
            nettyWebSocketStarter2.startNetty();
            logger.info("数据库连接成功");
        }catch (SQLException e){
            logger.error("数据库连接失败，请检查配置");
        }catch (RedisConnectionException e){
            logger.error("redis连接错误，请检查配置");
        }catch (Exception e){
            logger.error("服务启动失败",e);
        }

    }
}
